// const path = require("path");
// module.exports = {
//   mode: "none", // 模式，none | production | development
//   // 入口
//   entry: "./src/components/Lcc.js",
//   // 出口
//   output: {
//     path: path.resolve(__dirname, "dist"),
//     filename: "bundle.js"
//   }
// };
const htmlWebpackPlugin = require("html-webpack-plugin");
const vueLoaderPlugin = require("vue-loader/lib/plugin"); // 用于识别vue-loader文件
const happypack = require("happypack");
const os = require("os");
const happyThreadPool = happypack.ThreadPool({ size: os.cpus().length });

const loadsh = require("loadsh");
const customizer = function(objValue, srcValue) {
  if (Array.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
};
module.exports = env => {
  env = env || {}; // 防止报错
  let res = {
    entry: "./src/main.js",
    module: {
      rules: [
        {
          test: /.vue$/,
          use: "vue-loader"
        },
        {
          test: /.css$/,
          use: ["style-loader", "css-loader"] // 先用css-loader再用style-loader
          // 函数组合
          // compose 从右往左
          // pipe 从左往右
        },
        {
          test: /\.js$/,
          use: "happypack/loader?id=babel-loader",
          exclude: /node_modules/ // exclude写在babel.config.js，这里就可以不写了
        }
      ]
    },
    resolve: {
      extensions: [".js", ".json", ".vue"], // 默认扩展名
      alias: {
        // 别名
        vue: "vue/dist/vue.esm"
      }
    },
    plugins: [
      new htmlWebpackPlugin({
        template: "public/index.html",
        filename: "./index.html"
      }),
      new vueLoaderPlugin(),
      new happypack({
        // id标识happypack处理那一类文件
        id: "babel-loader",
        // 配置loader
        loaders: ["babel-loader"],
        // 共享进程池
        threadPool: happyThreadPool,
        // 日志输出
        verbose: true
      })
    ]
  };
  return loadsh.mergeWith(
    res,
    env.development
      ? require("./config/webpack.dev")
      : require("./config/webpack.pro"),
    customizer
  );
};
